package com.example.extend.configuration;

import com.example.extend.converter.StringToDateConverter;
import com.example.extend.interceptor.TestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;
import sun.nio.cs.ISO_8859_2;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
public class SpringMvcExtendConfiguration implements WebMvcConfigurer{

    //================================格式化器/转换器=============================================//
    @Autowired
    DateFormatter dateFormatter;
    @Autowired
    StringToDateConverter stringToDateConverter;
    @Override
    public void addFormatters(FormatterRegistry registry) {
       // registry.addFormatter(dateFormatter);
        registry.addConverter(stringToDateConverter);
    }
    @Bean
    public DateFormatter dateFormatter(){
        return new DateFormatter();
    }
    //================================消息转换器=============================================//
    @Autowired
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;
    @Autowired
    StringHttpMessageConverter stringHttpMessageConverter;
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        System.out.println("先调用configureMessageConverters");
        //这种方式，是将原本的jackson2HttpMessageConverter、stringHttpMessageConverter的值给修改了。
        jackson2HttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
        stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
        //而下面会复制一个新的添加到converters转换器表尾
        //converters.add(jackson2HttpMessageConverter);
        //converters.add(stringHttpMessageConverter);
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        System.out.println("后调用configureMessageConverters");
        //这里会将之前的设置的给覆盖掉
        jackson2HttpMessageConverter.setDefaultCharset(new ISO_8859_2());
        stringHttpMessageConverter.setDefaultCharset(new ISO_8859_2());
    }

    //================================拦截器=============================================//
    @Autowired
    TestInterceptor interceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //将拦截器添加到spring拦截器列表中，
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(interceptor);
        //配置该拦截器的路径
        interceptorRegistration.addPathPatterns("/");
        //设置该拦截器的拦截路径。
        //interceptorRegistration.addPathPatterns(List<多个拦截路径>);
        //设置该拦截器的排除拦截路径
        //interceptorRegistration.excludePathPatterns(List<多个拦截路径>);
        //设置该拦截器的拦截顺序order
        interceptorRegistration.order(1);
    }










    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        WebMvcConfigurer.super.configurePathMatch(configurer);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        WebMvcConfigurer.super.configureContentNegotiation(configurer);
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        WebMvcConfigurer.super.configureAsyncSupport(configurer);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        WebMvcConfigurer.super.configureDefaultServletHandling(configurer);
    }




    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        WebMvcConfigurer.super.configureViewResolvers(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        WebMvcConfigurer.super.addReturnValueHandlers(handlers);
    }


    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        WebMvcConfigurer.super.configureHandlerExceptionResolvers(resolvers);
    }

    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        WebMvcConfigurer.super.extendHandlerExceptionResolvers(resolvers);
    }

    @Override
    public Validator getValidator() {
        return WebMvcConfigurer.super.getValidator();
    }

    @Override
    public MessageCodesResolver getMessageCodesResolver() {
        return WebMvcConfigurer.super.getMessageCodesResolver();
    }
}
